﻿Shader "Custom/LightShader"
{
    Properties
    {
        [Toggle] _UseHalfLambert("Use Half Lambert", Range(0 , 1)) = 0
        _Color("Color", Color) = (1,1,1,1)
        _LightReverse("Light Reverse", Vector) = (1, 1, 0, 1)
        _LightIntensity("Light Intensity", Range(0, 1)) = 1
    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 200

        Pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            half _UseHalfLambert;
            float4 _Color;
            float4 _LightReverse;
            float _LightIntensity;

            struct appdata {
                float4 vertex : POSITION;
                float4 normal : NORMAL;
            };

            struct v2f {
                float4 pos : POSITION;
                float4 color : COLOR;
            };


            v2f vert(appdata v) {
                // lambert 光照模型比例值
                float lightRatio = max(0, dot(_LightReverse, v.normal));
                // half-lambert 光照模型比例值
                float halfLambertLightRatio = min(1, lightRatio * 0.5 + 0.5);
                // _UseHalfLambert >= 0.5 return 1 else return 0
                half halfLambertStep = step(0.5, _UseHalfLambert);
                


                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.color = _Color * lightRatio * _LightIntensity;
                return o;
            }

            half4 frag(v2f v) : COLOR{
                return v.color;
            }

            ENDCG
        }

        
    }
    FallBack "Diffuse"
}
